package com.jrfid.smartcabinet.demo.ui.inventory.adapter

import android.graphics.Color
import android.provider.CalendarContract.Colors
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.jrfid.smartcabinet.demo.R
import com.jrfid.smartcabinet.demo.databinding.ItemTagInfoBinding
import com.jrfid.smartcabinet.demo.model.TagInfoModel
import java.util.*
import kotlin.math.max
import kotlin.math.min

class TagInfoAdapter : BaseQuickAdapter<TagInfoModel, BaseDataBindingHolder<ItemTagInfoBinding>>(R.layout.item_tag_info) {

    init {

    }

//    private var ledEpcList = Collections.synchronizedList(mutableListOf<TagInfoModel>())

    private var ledTag: TagInfoModel? = null
    private var ledEpcPosition = -1

    fun getLedTag(): TagInfoModel? {
        return ledTag
    }

//    @Synchronized
//    fun getLedEpcList(): MutableList<TagInfoModel> {
//        return ledEpcList
//    }

//    @Synchronized
//    fun getLedEpcListIterator(): MutableIterator<TagInfoModel> {
//        return ledEpcList.iterator()
//    }

    fun clear() {
        ledTag = null
        ledEpcPosition = -1
        data.clear()
        notifyDataSetChanged()
    }

    fun selectLedEpc(item: TagInfoModel, position: Int) {
//        val index = ledEpcList.indexOfFirst { it.epc == item.epc }
//        if (index == -1) {
//            ledEpcList.add(item)
//        } else {
//            ledEpcList.removeAt(index)
//        }
        if (item.epc == ledTag?.epc) {
            ledTag = null
            notifyItemChanged(ledEpcPosition)
        } else {
            ledTag = item
            val last = ledEpcPosition
            ledEpcPosition = position
            notifyItemChanged(ledEpcPosition)
            notifyItemChanged(last)
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val view = View(context)
        view.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(120f))
        addFooterView(view)
    }

    override fun convert(holder: BaseDataBindingHolder<ItemTagInfoBinding>, item: TagInfoModel) {
        holder.itemView.alpha = 1f
        holder.dataBinding?.let {
//            val model = ledEpcList.find { it.epc == item.epc }
            it.cardView.setCardBackgroundColor(Color.parseColor(if (ledTag?.epc == item.epc) "#BEF1E2" else "#FFFFFF"))
            it.viewModel = item
            it.executePendingBindings()
        }
    }

    override fun addData(model: TagInfoModel) {
        var temp: TagInfoModel? = null
        var index: Int? = null
        data.forEachIndexed { i, item ->
            if (item.epc == model.epc) {
                temp = item
                index = i
                return@forEachIndexed
            }
        }
        if (temp == null) {
            if (model.epc.startsWith("00000000")) {
                super.addData(0, model)
            } else {
                super.addData(model)
            }

//            data.sort()
//            notifyDataSetChanged()
        } else {
            temp?.let {
                it.count += 1
                it.rssiMin = min(model.rssiMin!!, it.rssiMin!!)
                it.rssiMax = max(model.rssiMax!!, it.rssiMax!!)
            }
            setData(index!!, temp!!)
        }
    }

}